<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-select
          v-model="projectId"
          placeholder="建设项目"
          size="small"
          style="width: 300px" @change="handleProjectChange"
        >
          <el-option
            v-for="item in projectOptions"
            :key="item.projectId"
            :value="item.projectId"
            :label="item.projectName"></el-option>
        </el-select>
      </el-col>
    </el-row>
    <el-row style="padding-top: 10px;">
      <div id="map-container" class="map">
      </div>
    </el-row>
  </div>
</template>

<script>
  import { selectProjectList } from "@/api/project/project";
  import mapData from '@/data/map.json';

  export default {
    name: "projectMap",
    data() {
      return {
        projectId: null,
        // 项目列表
        projectOptions: [],
        // 地图
        map: null,
        mapData: mapData,
        data: null,
      }
    },
    mounted() {
      this.init();
    },
    methods: {
      // 初始化页面
      init() {
        // 加载项目下拉框
        selectProjectList().then(response => {
          if (response.projects.length > 0) {
            this.projectOptions = response.projects;
            this.projectId = response.projects[0].projectId;
            this.loadMap();
          }
        });
      },
      handleProjectChange() {
        this.reloadMap();
        this.loadMap();
      },
      loadMap() {
        if (this.mapData) {
          this.mapData.project.forEach(project => {
            if (project.projectId == this.projectId) {
              this.data = project.data;
              this.initMap(this);
            }
          });
        }
      },
      initMap(_this) {
        _this.map = new AMap.Map('map-container', {
          center: _this.data.center,
          zoom: _this.data.zoom,
          // viewMode: '3D',
        });
        // _this.map.setMapStyle("amap://styles/darkblue");

        // 线路图
        _this.data.lines.forEach(line => {
          let lines = new AMap.Polyline({
            path: line.data,
            // strokeColor: '#dc4646',
            strokeColor: line.color,
            strokeOpacity: 1,
            strokeWeight: 16,
            strokeStyle: "solid",
            lineJoin: "round",
            showDir: true,
            lineCap: "round",
            geodesic: true // 设置为true表示绘制大地线（弧线）
          });
          _this.map.add(lines);
        });
      },
      reloadMap() {
        if (this.map) {
          this.map.destroy();
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  .map {
    width: 100%;
    height: 75vh;
  }
  ::v-deep .amap-logo {
    display: none;
    opacity: 0 !important;
  }
  ::v-deep .amap-copyright {
    opacity: 0;
  }
</style>
